import { Component, OnInit, OnDestroy } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { ApiService, urlService } from 'src/app/core/api';
import { MessageService } from './../../../core/service/message.service';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-recharge-first',
  templateUrl: './recharge-first.component.html',
  styleUrls: ['./recharge-first.component.scss']
})
export class RechargeFirstComponent implements OnInit, OnDestroy {
  url: string;
  bankUrl: string;
  getCodeUrl: string;
  verifyCode: any = {};
  public isVisible: Boolean = false;
  public banks: Array<any> = [];
  public activeBank: any = {};
  public tempBank: any = {};
  constructor(
    private message: NzMessageService,
    private obs: MessageService,
    private router: Router,
    private storage: StorageService,
    private api: ApiService
  ) {

  }
  ngOnInit() {
    this.url = urlService(environment.type, '用户充值获取信息');
    this.bankUrl = urlService(environment.type, '平台收款银行账号列表');
    this.getCodeUrl = urlService(environment.type, '重新获取关联码');
    this.obs.sendMessage('充值');
    this.api.post(this.url, {}).subscribe((data: any) => {
      this.activeBank = data.data.platformBank;
      this.verifyCode = data.data.verifyCode;
    });
    this.api.post(this.bankUrl, {}).subscribe((data: any) => {
      this.banks = data.data.list;
    });
  }
  ngOnDestroy() {

  }
  /**
   * showBanks 显示弹出框
   */
  public showBanks() {
    this.isVisible = true;
    this.tempBank = this.activeBank;
  }
  /**
   * handleCancel 弹框-取消按钮
   */
  public handleCancel() {
    this.isVisible = false;
  }
  /**
   * handleOk 弹框-确认按钮
   */
  public handleOk() {
    this.isVisible = false;
    this.activeBank = this.tempBank;
  }
  /**
   * selectBank 选择收款银行
   */
  public selectBank(item) {
    this.tempBank = item;
  }
  /**
   * getCode 重新获取关联码
   */
  public getCode() {
    this.api.post(this.getCodeUrl, {}).subscribe((data: any) => {
      this.verifyCode = data.data;
    });
  }
  /**
   * goCreateRecharge 跳转充值订单页面
   */
  public goCreateRecharge() {
    sessionStorage.setItem('activeBank', JSON.stringify(this.activeBank));
    sessionStorage.setItem('verifyCode', this.verifyCode.verifyCode);
    this.router.navigate(['/member/recharge/second'], {});
  }
}

